#include<string>
#include<vector>
#include<array>
#include<unordered_map>
#include<algorithm>
#include<limits>
#include<stack>
class Solution {
public:
    std::vector<int> dailyTemperatures(std::vector<int>& temperatures) {
        std::stack<int> istack;
        int n = temperatures.size();
        std::vector<int> res(n);
        for (int i = 0; i < n; i++)
        {
            if (istack.empty()||temperatures[istack.top()] >= temperatures[i])
            {
                istack.push(i);
                continue;
            }
           while (!istack.empty()&&temperatures[istack.top()] < temperatures[i])
           {
               int index  = istack.top();
               istack.pop();
               res[index] = i-index;
           }
            istack.push(i);
        }
        while (!istack.empty())
        {
            int index = istack.top();
            istack.pop();
            res[index] = 0;
        }
        return res;
    }
};
